﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MBWebService.Core;
using MBWebService.Core.Classes;
using System.Collections.ObjectModel;
using MBWebService.Core.Client;

namespace MpchcClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<MpchcServiceClient> clients = new List<MpchcServiceClient>();
        private ObservableCollection<RemoteDevice> computers = new ObservableCollection<RemoteDevice>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void RefreshServers()
        {
            foreach (var server in this.computers)
            {
                // Only try and connect to a service if it's not already in the list...
                if (!this.clients.Any(c => c.Service.DnsName == server.DnsName))
                {
                    MpchcServiceClient client = new MpchcServiceClient(server, ClientDevice);
                    client.ServiceStarted += new EventHandler(client_ServiceStarted);
                    client.ServiceClosed += new EventHandler(client_ServiceClosed);
                    client.ConnectToService();
                    this.clients.Add(client);
                }
            }
        }

        void client_ServiceClosed(object sender, EventArgs e)
        {
            MpchcServiceClient client = sender as MpchcServiceClient;
            if (client != null)
            {
                clients.Remove(client);
            }
        }

        void client_ServiceStarted(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void okBtn_Click(object sender, RoutedEventArgs e)
        {
            // Add to list...
            string val = computerTxt.Text;
            string validation = ValidateRemoteComputer(val);
            if (string.IsNullOrEmpty(validation))
            {
                ShowNewComputer(false);
                computers.Add(new RemoteDevice { DnsName = val, BasePort = int.Parse(remotePortTxt.Text) });
                computerTxt.Text = string.Empty;
                RefreshServers();
            }
            else
            {
                MessageBox.Show(validation);
            }
        }

        #region Validation Methods

        private string ValidateRemoteComputer(string name)
        {
            string retVal = string.Empty;

            if (string.IsNullOrEmpty(name))
                retVal += "Name cannot be emtpy.\r\n";

            if (computers.Any(o => o.DnsName == name))
                retVal += "Device already exists in list, choose another.\r\n";

            retVal += ValidatePort(remotePortTxt.Text);

            return retVal;
        }

        private string ValidatePort(string potentialPort)
        {
            string retVal = string.Empty;

            int port;
            if (!int.TryParse(potentialPort, out port))
                retVal += "Incorrect value for port.\r\n";

            return retVal;
        }

        private void addServiceBtn_Click(object sender, RoutedEventArgs e)
        {
            ShowNewComputer(true);
        }

        #endregion

        private void ShowNewComputer(bool show)
        {
            Visibility vis = show ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
            okBtn.Visibility = vis;
            computerTxt.Visibility = vis;
            remotePortTxt.Visibility = vis;
            remoteNameLbl.Visibility = vis;
            remotePortLbl.Visibility = vis;

            if (show)
            {
                remotePortTxt.Text = AppSettings.UserSettings.BasePort.ToString();
            }
        }

        private static MBWebService.Server.Proxy.PlaybackDevice ClientDevice
        {
            get
            {
                return new MBWebService.Server.Proxy.PlaybackDevice
                {
                    IsMediaCenter = false,
                    IsRemote = true,
                    Name = string.Format("{0}-mpchc", AppSettings.SessionName)
                };
            }
        }
    }
}
